package cn.lcj.org.service.impl;

import cn.lcj.basic.service.impl.BaseServiceImpl;
import cn.lcj.basic.utils.BaiduAiUtils;
import cn.lcj.org.domain.Employee;
import cn.lcj.org.domain.Shop;
import cn.lcj.org.domain.ShopAuditLog;
import cn.lcj.org.dto.ShopDto;
import cn.lcj.org.mapper.EmployeeMapper;
import cn.lcj.org.mapper.ShopAuditLogMapper;
import cn.lcj.org.mapper.ShopMapper;
import cn.lcj.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //一、校验
        //1、商店空值校验
        if (StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getAddress())) {
            throw new RuntimeException("店铺信息不完整，请补全信息");
        }
        //2、两次密码不一致校验
        if (!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())) {
            throw new RuntimeException("两次密码不相等");
        }
        //3、当前店铺是否入驻过
        Shop dbshop = shopMapper.findByNameAndAdress();
        if (dbshop != null) {
            throw new RuntimeException("该用户已经注册了店铺，请直接登录，忘记密码请联系客服");
        }
        //二、百度Ai智能审核【店铺、logo是否合法】
        if (!BaiduAiUtils.TextCensor(shop.getName())) {
            throw new RuntimeException("文本中含有敏感词，请重新输入");
        }
        if (!BaiduAiUtils.ImgCensor("http://123.207.27.208" + shop.getLogo())) {
            throw new RuntimeException("图片不合法,请重新输入");
        }
        //三、添加到数据库
        //1.先添加员工信息
        //1.1获取员工信息
        Employee admin = shop.getAdmin();
        //1.2添加员工信息
        employeeMapper.save(admin);//shopid=null 需要在xml里save方法加三个主键自增的属性
        // useGeneratedKeys="true" keyProperty="id" keyColumn="id"
        //2.再添加店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.save(shop); //会返回自增长id
        //3.将店铺id更新到员工工作列表
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    @Transactional
    public void auditReject(ShopAuditLog log) throws MessagingException {
        //1.改状态：1待审核2.审核成功 待激活
        shopMapper.updateState(4, log.getShopId());
        //2.添加审核日志
        log.setState(4);
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);
        //3.发邮件给店长通知他去激活
        //创建复杂邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        //发送复杂邮件的工具类
        message.setFrom("1907326859@qq.com");//平台邮箱
        message.setSubject("店铺激活邮件");
        Shop shop = shopMapper.loadById(log.getShopId());
        message.setText("店铺入驻失败，请重新入驻!!!");

       /* //添加附件
        helper.addAttachment("p7.jpg",new File("C:\\Users\\Administrator\\Pictures\\photos\\p7.jpg"));
        helper.addAttachment("g1.gif", new File("C:\\Users\\Administrator\\Pictures\\g1.gif"));*/
        //收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        message.setTo(employee.getEmail());//改店长邮箱为真实邮箱
        javaMailSender.send(message);
    }


    @Override
    @Transactional
    public void auditPass(ShopAuditLog log) throws MessagingException {
        //1.改状态：1待审核2.审核成功 待激活
        shopMapper.updateState(2, log.getShopId());
        //2.添加审核日志
        log.setState(2);
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);
        //3.发邮件给店长通知他去激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("1907326859@qq.com");//平台邮箱
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208/" + log.getShopId() + "'>" +
                "<a href='http://127.0.0.1:8080/shop/active/" + log.getShopId() + "'>点击该链接激活</a>", true);
       /* //添加附件
        helper.addAttachment("p7.jpg",new File("C:\\Users\\Administrator\\Pictures\\photos\\p7.jpg"));
        helper.addAttachment("g1.gif", new File("C:\\Users\\Administrator\\Pictures\\g1.gif"));*/
        //收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(employee.getEmail());//改店长邮箱为真实邮箱
        javaMailSender.send(mimeMessage);
    }

    /**
     * 人工审核之后激活邮件
     * @param id
     * @return
     */
    @Override
    @Transactional
    public String activeState(Long id) {
        Shop shop = shopMapper.loadById(id);
         shop.setState(3);
        shopMapper.update(shop);
        return "恭喜你，激活成功";
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}